package com.example.demo.service.order;

import com.example.demo.common.IsObjectEmpty;
import com.example.demo.domain.order.Payment;
import com.example.demo.domain.order.SaleOrder;
import com.example.demo.domain.userInfo.UserInfo;
import com.example.demo.mapper.order.PaymentMapper;
import com.example.demo.mapper.order.SaleOrderMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

@Service
public class PaymentServiceImpl implements  PaymentService{
    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private SaleOrderMapper saleOrderMapper;

    @Override
    public int deleteByPrimaryKey(Integer payId) {
        return this.deleteByPrimaryKey(payId);
    }

    @Override
    public int insert(Payment record) {
        SaleOrder saleOrder = saleOrderMapper.selectBySaleOrderNo(record.getSaleorderno());
        //改变支付状态
        saleOrder.setPaymentType(record.getPayType());
        saleOrder.setPayState(2);
        //设置支付时间
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        saleOrder.setEndTime(timestamp);
        record.setPayTime(timestamp);
        UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        if (IsObjectEmpty.checkObjAllFieldsIsNull(user)) {
            SecurityUtils.getSubject().logout();
        }
        record.setCashierId(user.getUid());
        saleOrderMapper.updateByPrimaryKeySelective(saleOrder);
        return this.paymentMapper.insertSelective(record);
    }

    @Override
    public int insertSelective(Payment record) {
        return this.paymentMapper.insertSelective(record);
    }

    @Override
    public Payment selectByPrimaryKey(Integer payId) {
        return this.paymentMapper.selectByPrimaryKey(payId);
    }

    @Override
    public int updateByPrimaryKeySelective(Payment record) {
        return this.paymentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Payment record) {
        return this.paymentMapper.updateByPrimaryKey(record);
    }
}
